(function() {
    angular.module('app').component('navComponent', {
        templateUrl: 'app/components/nav.component/nav.component.html',
        controller: navCtrl,
        controllerAs: 'vm'
    });

    navCtrl.$inject = ['util', '$rootScope', '$timeout', 'navComponentService', 'userService', 'util'];

    function navCtrl(util, $rootScope, $timeout, navComponentService, userService, util) {
        var vm = this;
        // console.log('nav-component js');



        vm.industry = {
            array: [],
            title: '',
            change: function() {
                $("#dropdownicon").dropdown();
                $("#dropdownicon").dropdown('open');
            },
            select: function(index) {
                if (this.title == this.array[index]) {
                    return
                } else {
                    this.title = this.array[index];
                    $timeout(function() {
                        $rootScope.$emit('industry_cat_EV', index);
                        $rootScope.userInfo.industry_selectIndex = index;
                    }, 10);

                }
            },
            close: function() {
                $("#dropdownicon").dropdown('close');
            }
        }
        $(".button-collapse").sideNav();



        vm.showSliderNav = showSliderNav; //function

        vm.user = {}; //userinfo


        console.log($rootScope.userInfo);

        vm.$onInit = function() {


            vm.sourceApi = util.sourceApi();
            console.log(vm.sourceApi);

            angular.element(document.body).addClass(vm.sourceApi.theme);



            if (document.body.clientWidth >= 1400) {

                if (util.$localStorage().has('nav_ping') && !util.$localStorage().get('nav_ping')) {
                    
                    vm.ping = false;
                } else {

                    vm.ping = true;
                    angular.element(document.body).addClass('nav-aside-hover');
                }

            } else {
                vm.ping = false;
            }


        };
        vm.page = '/industry';

        vm.shop = shop;
        vm.exit = _exit; //function


        //路由跳转成功，nav-item active
        $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams, options) {

            // console.log(toState);

            // console.log(toParams);

            // console.log(fromState);

            // console.log(fromParams);

            // console.log(options);





            // console.log('开始刷新');

            vm.page = toState.url || '/industry';


            if ($.isEmptyObject(vm.user)) {


                console.log('-------------state---------');
                console.log($rootScope.userInfo);


                vm.industry.title = util.returnUserIndustry($rootScope.userInfo);

                if (angular.isArray(vm.industry.title)) {
                    vm.industry.array = vm.industry.title;
                    vm.industry.select(0);
                }
                vm.user = $rootScope.userInfo;
                _modal_deadLine(vm.user.deadLine);
                _free_version(vm.user.version);
                // console.log('---------vn--------------');
                // console.log(vm.user);

            }
        });

        // 权限限制 // 路由开始
        $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams, options) {
            

            console.log($rootScope.userInfo);
            //行业控制
            if ($rootScope.userInfo && $rootScope.userInfo.version.indexOf('-v1') == -1) {

                var itemLevel1Category = $rootScope.userInfo.itemLevel1Category;

                // console.log('user-----------------------------');
                // console.log(itemLevel1Category == null);
                // console.log(typeof itemLevel1Category);

                //行业未选择
                if (itemLevel1Category == null || itemLevel1Category == '' || itemLevel1Category == "NULL") {
                    //组件industry.select.component
                    $rootScope.$emit('industry_select_EV');
                    event.preventDefault();
                }
            }
            //版本url控制 标准版限制访问
            if ($rootScope.userInfo && $rootScope.userInfo.version.indexOf('pro') == -1) {
                // console.log(typeof (path.originalPath));
                if (typeof toState.url != "undefined") {
                    if (toState.url == '/brand' || toState.url == '/newProduct' || toState.url == '/attr' || toState.url == '/words' || toState.url == '/redNet') {
                        event.preventDefault();
                    }
                }

            }


        });
        // 路由错误
        $rootScope.$on('$stateChangeError', function(event, next, current, error) {

            console.log('route error');
            // console.log(event);
            // console.log(next);
            // console.log(current)

        });

        function _modal_deadLine(time) {
            if (!/\d{4}-\d{2}-\d{2}\s\d{2}-\d{2}-\d{2}/gi.test(time)) {
                time += ':00';
            }

            var remainderMillisecondsForDay = new Date(time).getTime() - new Date().getTime();
            var day = Math.floor(remainderMillisecondsForDay / (24 * 60 * 60 * 1000));

            var remainderMillisecondsForHour = remainderMillisecondsForDay - (day * 24 * 60 * 60 * 1000);
            var hour = Math.floor(remainderMillisecondsForHour / (60 * 60 * 1000));

            var remainderMillisecondsForMinute = remainderMillisecondsForHour - (hour * 60 * 60 * 1000);
            var minutes = Math.floor(remainderMillisecondsForMinute / (60 * 1000));





            if (day > 15) {
                return;
            }
            if ((day <= 5 || day == 15) && !window.localStorage.getItem('expiration_remind_' + day + '_days')) {
                var modal = {
                    title: '账号到期提醒',
                    content: {
                        text: '<span>您的账号距离到期剩余' + day + '天' + hour + '小时' + minutes + '分,请您及时续费!。</span>'
                    }
                }

                $rootScope.$emit('MODAL_COMPONENT_EV', modal);
                window.localStorage.setItem('expiration_remind_' + day + '_days', 'true');
            }
        }

        function _free_version(version) {
            if (version == 'free') {
                var modal = {
                    title: '试用账号提醒',
                    content: {
                        text: '<span>您当前使用的是试用账号,请您点击页面左下角购买按钮及时续费,购买完成后刷新即可开始试用。</span>'
                    }
                }

                $rootScope.$emit('MODAL_COMPONENT_EV', modal);
            }
        }




        function shop() {
            if (vm.user.channel == '1') {
                window.open("http://fw.yunqi.shopex.cn/detail/745");
            }
        }

        function _exit() {
            navComponentService.loginOut(vm.user);
        }



        function showSliderNav() {
            if (vm.ping) { //
                angular.element(document.body).removeClass('nav-aside-hover');
                // $('#pc_demo .site-menu-item a').popup();
            } else {
                angular.element(document.body).addClass('nav-aside-hover');
                // $('#pc_demo .site-menu-item a').popup('destroy');
            }
            vm.ping = !vm.ping;

            util.$localStorage().set('nav_ping', vm.ping);
        }
    }
})();
